package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminDishController")
@RequestMapping("admin/dish")
@Slf4j
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping("page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.getPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")//此处是wx端根据左侧导航栏id清除里面的内容
    public Result save(@RequestBody DishDTO dishDTO) {
        dishService.saveDishWithFlavor(dishDTO);

        return Result.success();
    }

    @PostMapping("status/{status}")
    @ApiOperation("启用禁用菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status,Long id) {
        dishService.updateStatus(status,id);
        return Result.success();
    }

    @GetMapping("{id}")
    @ApiOperation("根据菜品Id查询菜品信息(在修改界面回显数据)")
    public Result<DishDTO> getDishById(@PathVariable Long id) {
        DishDTO dishDTO = dishService.getInfoById(id);
        return Result.success(dishDTO);
    }

    @PutMapping
    @ApiOperation("修改菜品信息")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO) {
        dishService.update(dishDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除菜品信息")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result deleteDishById(@RequestParam List<Long> ids) {
        dishService.deleteDishById(ids);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }


}
